package com.example.lemis.common.config.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MyBatis-Plus 自动填充规则
 *  - INSERT 时填 registerTime、deleted
 *  - UPDATE 时填 lastLoginTime
 */
@Slf4j
@Component       // 关键：让 Spring 扫描并注册成 Bean
public class MyMetaObjectHandler implements MetaObjectHandler {

    /** 插入触发 */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "registerTime",
                LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "gmt_created",
                LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "deleted",
                () -> Boolean.FALSE, Boolean.class);

    }

    /** 更新触发 */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "lastLoginTime",
                LocalDateTime::now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, "gmt_modified",
                LocalDateTime::now, LocalDateTime.class);
    }


}
